<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/12
 * Time: 15:10
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Drive extends Base {


    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.try_user_name|a.try_user_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($province)&&$province!="")
            {
                $map['a.try_sheng'] = $province;
            }

            if(isset($city)&&$city!="")
            {
                $map['a.try_shi'] = $city;
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.try_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.try_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.try_time'] = ['between time',[$start,$end]];
            }
            if(isset($try_status) && $try_status != ''){
                $map['a.try_status'] = $try_status;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('drive')->alias('a')
                ->join('up_dealer ud','ud.dealer_id = a.try_dealer_id','left')
                ->join('up_car_series uc','uc.cs_id = a.try_car_id','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('drive')->alias('a')
                ->join('up_dealer ud','ud.dealer_id = a.try_dealer_id','left')
                ->join('up_car_series uc','uc.cs_id = a.try_car_id','left')
                ->where($map)
                ->field('a.*,ud.dealer_name,uc.cs_name')
                ->page($NowPage,$limits)
                ->order('try_time desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                $sheng = Db::table('up_area')->where('district_id',$vo['try_sheng'])->value('district');
                $shi = Db::table('up_area')->where('district_id',$vo['try_shi'])->value('district');
                $vo['address'] = $sheng.$shi;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign([
            'province' => $province,
        ]);
        return $this->fetch();
    }
    
    /**
     * 处理
     * @return \think\response\Json
     */
    public function handle(){
        $try_id = input('id');
        $res = Db::table('up_drive')->where('try_id', $try_id)->setField('try_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '处理成功']);
        }else{
            return json(['code' => 100, 'msg' => '处理失败']);
        }
    }
    
    /**
     * 备注
     * @return mixed|\think\response\Json
     */
    public function remark(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $try_content = input('try_content');
            $data = [
                'try_content' => $try_content
            ];
            $res = Db::table('up_drive')->where('try_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '处理成功']);
            }else{
                return json(['code' => 100, 'msg' => '处理失败']);
            }
        }
        $try_id= input('try_id');
        $this->assign('id',$try_id);
        return $this->fetch();
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function tryDel(){
        $try_id = input('id');
        $res = Db::name('drive')->where('try_id',$try_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    
}